from .constants import VALID_CITIES, CITY_CENTERS

def validate_city_pair(province, city):
    """校验省-市匹配关系"""
    return any(
        p == province and c == city 
        for (p, c), _ in VALID_CITIES.items()
    )

def get_city_radius_km(city_code):
    """获取城市覆盖半径（公里）"""
    return CITY_CENTERS.get(city_code, {}).get('radius_km', 30)  # 默认30公里

def get_nearby_cities(target_city_code, max_distance=100):
    """模拟获取邻近城市（基于行政区划代码的简单实现）"""
    target = CITY_CENTERS.get(target_city_code)
    if not target:
        return []
    
    return [
        (code, data) 
        for code, data in CITY_CENTERS.items()
        if code != target_city_code
        # 简单距离计算（实际项目建议用RedisGeo或MySQL空间函数）
        and abs(data['latitude'] - target['latitude']) < 1.0
        and abs(data['longitude'] - target['longitude']) < 1.0
    ]

def validate_and_normalize_address(province, city, district):
    """验证并标准化地址信息"""
    from .constants import PROVINCES, CITIES
    
    # 省级验证
    if province not in PROVINCES.values():
        province = next((p for p in PROVINCES.values() if province in p), province)
    
    # 市级验证
    city_key = next((k for k, v in CITIES.items() if v == city), None)
    if not city_key:
        city = next((c for c in CITIES.values() if city in c), city)
    
    return province, city, district

def get_city_center(city_name):
    """
    根据城市名获取大致坐标（示例实现）
    实际项目中可能需要调用地理编码API（如高德、百度地图API）
    """
    # 示例：预设一些城市的中心坐标
    city_coords = {
        "北京": (39.9042, 116.4074),
        "上海": (31.2304, 121.4737),
        # 其他城市...
    }
    # 返回默认坐标（如(0,0)）如果城市未找到
    return city_coords.get(city_name, (0.0, 0.0))